﻿//-----------------------------------------------------------------------------
// <copyright file="TestableViewElement.cs" company="William E. Kempf">
//     Copyright (c) William E. Kempf.
// </copyright>
//-----------------------------------------------------------------------------

namespace SimpleTest.TestArtifacts
{
    using System;
    using System.ComponentModel.Design;
    using System.Windows;
using System.Collections.Generic;
    using Onyx.ComponentModel;

    /// <summary>
    /// Provides a <em>view element</em> that can be used within unit tests.
    /// </summary>
    internal class TestableViewElement : FrameworkElement, IRegisterServices
    {
        private readonly Dictionary<Type, object> services = new Dictionary<Type, object>();

        /// <summary>
        /// Adds the specified service to the service container.
        /// </summary>
        /// <param name="serviceType">The type of service to add.</param>
        /// <param name="serviceInstance">An instance of the service type to add. This object must implement or inherit
        /// from the type indicated by the <paramref name="serviceType"/> parameter.</param>
        public void AddService(Type serviceType, object serviceInstance)
        {
            this.services.Add(serviceType, serviceInstance);
        }

        /// <summary>
        /// Gets the service object of the specified type.
        /// </summary>
        /// <param name="serviceType">An object that specifies the type of service object to get.</param>
        /// <returns>
        /// A service object of type <paramref name="serviceType"/>.
        /// -or-
        /// null if there is no service object of type <paramref name="serviceType"/>.
        /// </returns>
        public object GetService(Type serviceType)
        {
            object serviceInstance;
            this.services.TryGetValue(serviceType, out serviceInstance);
            return serviceInstance;
        }

        public void RegisterServices(IServiceRegistry registry)
        {
            foreach (var registration in this.services)
            {
                registry.RegisterService(registration.Key, registration.Value);
            }
        }
    }
}
